//
//  HHContractListViewController.m
//  HHBroker
//
//  Created by Herui on 9/5/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHContractListViewController.h"
#import "HHMyContractTableViewCell.h"
#import "HHContractListInfoModel.h"
#import "HHContractDetailViewController.h"

@interface HHContractListViewController ()

@end

@implementation HHContractListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - Fun
- (void)loadTableViewDataSourceWithState:(HHRefreshState)state
{
    //MBProgressHUD *hub;
    if (state == HHRefreshStateNormal) {
        // hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    
    NSString *urlString = [NSString stringWithFormat:@"%@?p=%ld", KUrlContractList, (long)self.page];
    [HttpRequestManager getWithUrlString:urlString
                                 success:^(id response) {
//                                     if (hub) {
//                                         [hub hide:YES];
//                                     }
                                     [self getJSONWithResponse:response tag:state];
                                     [self endRefreshing];
                                     
                                 }
                                 failure:^(NSError *error) {
//                                     if (hub) {
//                                         [hub hide:NO];
//                                     }
//                                     MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                                     hub.mode = MBProgressHUDModeText;
//                                     hub.labelText = @"网络有点问题!";
//                                     [hub hide:YES afterDelay:1];
                                     [self endRefreshing];
                                     
                                 } cache:NO];
}

- (void)getJSONWithResponse:(id)response tag:(NSInteger)tag
{
    NSInteger code = [[response objectForKey:@"code"] integerValue];
    if (code == 1) {
        id data = [response objectForKey:@"data"];
        if ([data isKindOfClass:[NSArray class]]) {
            NSArray *dataArray = (NSArray *)data;
            for (NSDictionary *dic in dataArray) {
                HHContractListInfoModel *model = [[HHContractListInfoModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArray addObject:model];
            }
        }
        if (self.dataArray.count == 0) {
//            MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//            hub.mode = MBProgressHUDModeText;
//            hub.labelText = @"暂无合同";
//            [hub hide:YES afterDelay:1];
        }
        if (self.dataArray.count < 10) {
            self.isNotRefreshData = YES;
        } else {
            self.isNotRefreshData = NO;
        }
        [self.tableView reloadData];
    }
}

#pragma mark - Delegate

#pragma mark - UITableViewDataSource

- (NSInteger) tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell*) tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    static NSString *reuseCell = @"reuseCell";
    HHMyContractTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HHMyContractTableViewCell" owner:self options:nil] lastObject];
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HHContractListInfoModel *model = self.dataArray[indexPath.row];
    return [HHMyContractTableViewCell heightForModel:model];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HHContractListInfoModel *model = self.dataArray[indexPath.row];
    HHContractDetailViewController *detail = [[HHContractDetailViewController alloc] init];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];

}








@end
